import { run } from './algo/run.js'
import { Delta,Data} from './data.js'
const Shapes ={}
for (let dataIte of Data) {

    const headItem = document.createElement('div')
    headItem.classList.add('column')
    headItem.innerHTML = dataIte.name
    const headInput = document.createElement('input')

    headInput.classList.add('input')
    headInput.type='number'
    headInput.value=`${Delta[dataIte.id]??0}`
    const DataIte = dataIte
    headInput.onchange=()=>{
         Delta[DataIte.id]=Number(headInput.value)
         process()
    }
    headItem.appendChild(headInput)
    head.appendChild(headItem)

    const bodyItem = document.createElement('div')
    bodyItem.classList.add('column')

    const shap = document.createElement('div')
    shap.classList.add('shape')
    
    bodyItem.appendChild(shap)
    Shapes[dataIte.id]=shap
    body.appendChild(bodyItem)
}
function process() {
    const Data = run()
    const MaxValue = Math.max(...Data.raw.map(ite => Data.value[ite.id]))
    const MinValue = Math.min(...Data.raw.map(ite => Data.value[ite.id]))
    const baseHeight = 0
    function calcShapHeight(value) {
        if (MaxValue === MinValue) {
            return 100
        }
        return baseHeight + (100 - baseHeight) * (value - MinValue) / (MaxValue - MinValue)
    }  
    for (let dataIte of Data.raw) {
        const bodyItem = document.createElement('div')
        bodyItem.classList.add('column')
        const shape = Shapes[dataIte.id]
        const Value = Data.value[dataIte.id]
        shape.style.height = `${calcShapHeight(Value)}%`
        shape.innerHTML=''
        const valueEl = document.createElement('span')
        valueEl.innerHTML= Number(Value.toFixed(4))
        shape.appendChild(valueEl)
    }
}

process()
